#include <bits/stdc++.h>
using namespace std;
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using ll = long long;
ll lcm(ll a,ll b)
{
    return a * b / __gcd(a, b);
}
void solve()
{
    ll n;
    cin >> n;
    vector<ll> p(n+1),s(n+1),res(n+1);
    for(ll i=1;i<=n;i++)
    {
        cin >> p[i];
    }
    for(ll i=1;i<=n;i++)
    {
        cin >> s[i];
    }
    if(p[n]!=s[1])
    {
        no;
        return;
    }
    for(ll i=1;i<=n;i++)
    {
        res[i] = p[i];
    }
    for (ll i = n; i >= 1; i--)
    {
        ll lc = lcm(res[i], s[i]);
        res[i] = lc;
    }
    ll cnt = res[1];
    for(ll i=1;i<=n;i++)
    {
        cnt = __gcd(res[i], cnt);
        if(cnt!=p[i])
        {
            no;
            return;
        }
    }
    cnt = s[n];
    for (ll i = n; i >= 1; i--)
    {
        cnt = __gcd(res[i], cnt);
        if (cnt != s[i])
        {
            no;
            return;
        }
    }
    yes;
}
int main()
{
    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
   return 0;
}
